//引入vue和vuex
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
import {
	getPersonInfo,
	addLogin,
	loginWithCode,
	bindPhone,
} from "@/api/user.js";
import {
	system,
	activeSystem
} from "@/api/index.js";

const store = new Vuex.Store({
	//全局变量定义
	state: {
		userInfo: uni.getStorageSync("userInfo") ?
			uni.getStorageSync("userInfo") : "",
		token: uni.getStorageSync("token") ? uni.getStorageSync("token") : "",
		fileData: '', //资料数据
		collectFileData: '', //集合数据
		systemData:'',//系统数据
		activeSystemData:''//激活码相关设置
	},
	mutations: {
		SET_USERINFO(state, data) {
			state.userInfo = data;
			uni.setStorageSync("userInfo", state.userInfo);
		},
		SET_TOKEN(state, data) {
			state.token = data;
			uni.setStorageSync("token", state.token);
		},
		SET_FILEDATA(state, data) {
			state.fileData = data;
		},
		SET_COLLECTFILEDATA(state, data) {
			state.collectFileData = data;
		},
		SET_SYSTEMDATA(state, data) {
			state.systemData = data;
		},
		SET_ACTIVESYSTEMDATA(state, data) {
			state.activeSystemData = data;
		},

	},
	actions: {
		getFileData({
			commit
		}, data) {
			commit("SET_FILEDATA", data);
		},
		getCollectFileData({
			commit
		}, data) {
			commit("SET_COLLECTFILEDATA", data);
		},
		login({
			dispatch,
			commit
		}, loginData) {
			return new Promise((resolve, reject) => {
				commit("SET_TOKEN", "");
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event) {
						const {
							code
						} = event
						
						loginWithCode({
							code
						}).then((response) => {
							const {
								data
							} = response;
							commit("SET_TOKEN", data.token);
							dispatch('getSystem');
							dispatch('getUserInfo');
							resolve()
						}).catch((error) => {
							reject(error);
						});
					},
					fail: function(err) {
						// 登录授权失败
						// err.code是错误码
					}
				})
				
			});
		},
		//绑定手机号
		handleBindPhone({
			commit
		},loginData){
			bindPhone({
					encryptedData: loginData.encryptedData,
					iv: loginData.iv,
					code: loginData.code
				})
				.then((response) => {
					const {
						data
					} = response;
					dispatch('getUserInfo');
					resolve()
				})
				.catch((error) => {
					reject(error);
				});
		},
		getUserInfo({
			commit
		}) {
			return new Promise((resolve, reject) => {
				getPersonInfo().then(response => {
					const {
						data
					} = response;
					commit("SET_USERINFO", data)
				})
			})
		},
		getSystem({
			commit
		}){
			return new Promise((resolve, reject) => {
				system().then(response => {
					const {
						data
					} = response;
					commit("SET_SYSTEMDATA", data)
				})
			})
		},
		getActiveSystem({
			commit
		}){
			return new Promise((resolve, reject) => {
				activeSystem().then(response => {
					const {
						data
					} = response;
					commit("SET_ACTIVESYSTEMDATA", data)
				})
			})
		},
		signOut({
			commit
		}) {
			commit("SET_TOKEN", "");
			commit("SET_USERINFO", "");
		},
	},
});
export default store;